package com.solome.app.lication;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.solome.base.AbstractScreen;
import com.solome.device.IConfig;
import com.solome.oc.CodeUi;
import com.solome.util.FontFactory;

public class FirstPage extends AbstractScreen {
	private final AssetManager manager = new AssetManager();

	private final String image_bg = "app/loginback.png";
	private final String image_text = "app/text.png";
	private final String image_logo = "app/logo.png";
	private final String image_button = "app/button.png";
	Animation animation;
	float stateTime;
	BitmapFont bitmapFont = FontFactory.getInstance().init("", 15);
	WidgetGroup left_Widget = new WidgetGroup();

	WidgetGroup right_Widget;

	public FirstPage(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		// manager.load(loading_black, Texture.class);
		manager.load(image_bg, Texture.class);
		manager.load(image_text, Texture.class);
		manager.load(image_logo, Texture.class);
		manager.load(image_button, Texture.class);
		manager.finishLoading();
		//
		Texture texture = manager.get(image_bg, Texture.class);
		Image image = new Image(texture);
		this.add(image);

		float y = 0;
		texture = manager.get(image_logo, Texture.class);
		image = new Image(texture);
		System.out.println("Gdx.graphics.getHeight()=" + this.getHeight());
		y = this.getHeight() - image.getHeight() - 20;
		image.setPosition(this.getWidth() / 2, y, Align.center);
		this.add(image);

		y -= image.getHeight() + 20;
		TextFieldStyle fieldStyle = IConfig.skin.get(TextFieldStyle.class);
		fieldStyle.background = new TextureRegionDrawable(new TextureRegion(
				manager.get(image_text, Texture.class)));
		if (fieldStyle.font != null)
			fieldStyle.font.dispose();
		fieldStyle.font = bitmapFont;
		fieldStyle.fontColor = Color.BLACK;
		if (fieldStyle.messageFont != null)
			fieldStyle.messageFont.dispose();
		fieldStyle.messageFontColor = Color.CYAN;
		fieldStyle.messageFont = bitmapFont;

		TextField textfield = new TextField("", fieldStyle);
		textfield.setMaxLength(50);
		textfield.setSize(this.getWidth() - 30, 43);
		textfield.setPosition((this.getWidth() - textfield.getWidth()) / 2, y);
		textfield.setMessageText("请输入帐号");
		textfield.setAlignment(Align.center);
		textfield.setName("1");
		this.add(textfield);

		y -= 43;
		textfield = new TextField("", fieldStyle);
		textfield.setMaxLength(50);
		textfield.setSize(this.getWidth() - 30, 43);
		textfield.setPosition((this.getWidth() - textfield.getWidth()) / 2, y);
		textfield.setMessageText("请输入密码");
		textfield.setAlignment(Align.center);
		textfield.setPasswordCharacter('*');
		textfield.setPasswordMode(true);
		textfield.setName("2");
		this.add(textfield);

		y -= 43;
		TextureRegionDrawable button = new TextureRegionDrawable(
				new TextureRegion(manager.get(image_button, Texture.class)));

		TextButtonStyle buttonStyle = new TextButtonStyle(button, button,
				button, bitmapFont);
		buttonStyle.checkedFontColor = Color.RED;
		buttonStyle.checkedOverFontColor = Color.WHITE;
		buttonStyle.disabledFontColor = Color.CORAL;
		buttonStyle.downFontColor = Color.GOLD;
		buttonStyle.fontColor = Color.WHITE;
		TextButton textButton = new TextButton("登 陆", buttonStyle);
		textButton.setSize(this.getWidth() - 30, 43);
		textButton
				.setPosition((this.getWidth() - textButton.getWidth()) / 2, y);
		textButton.addListener(new ChangeListener() {
			public void changed(ChangeEvent event, Actor actor) {
				game.setScreen(new CodeUi(game));
			}
		});
		this.add(textButton);
		this.setInputAdapter();
		this.setDebugGameInfo(true);
	}

	int index = 0;
	boolean isX = false;

	@Override
	public void draw(float delta) {
		// Image image_loading_write = (Image) this.get(0);
		// Image image_loading_black = (Image) this.get(1);
		// if (image_loading_write != null && image_loading_black != null) {
		// image_loading_write.setOrigin(Align.center);
		// image_loading_write.setRotation(-index);
		// // image_loading_black.setOrigin(Align.center);
		// // image_loading_black.setRotation(-index);
		// if ((index += 1) >= 360) {
		// index = 0;
		// }
		// }
	}

	@Override
	public void free() {
		// TODO Auto-generated method stub
		manager.dispose();
	}

	float touchScale = 0;

	@Override
	public boolean onTouchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		touchScale = 1;
		left_Widget.setScale(1);
		return super.onTouchDown(screenX, screenY, pointer, button);
	}

	@Override
	public boolean onTouchMove(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return super.onTouchMove(screenX, screenY, pointer);
	}

	@Override
	public boolean onTouchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		touchScale = 1;
		left_Widget.setScale(1);
		return super.onTouchUp(screenX, screenY, pointer, button);
	}
}
